package com.coocaa.ops.plus.core.third.visionhub;

import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.plus.core.third.ThirdServiceProperties;
import com.coocaa.ops.plus.core.third.visionhub.handler.PosterSimilarityHandler;
import com.coocaa.ops.plus.core.third.visionhub.model.PosterSimilarityResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * @author bjh
 * @description
 * @date 2020/12/22 19:49
 */
@Service
public class VisionFeignClientService {
    @Autowired
    private VisionFeignClient visionHubFeignClient;
    @Autowired
    private ThirdServiceProperties properties;


    public String getImgFaceInfo(String posterUrl) {
        try {
            return visionHubFeignClient.getImgFaceInfo(new URI(properties.getVisionServiceUrl()), posterUrl);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return "";
        }
    }


    public String textDetect(MultipartFile file, String type, Integer withPosition) {
        try {
            return visionHubFeignClient.textDetect(new URI(properties.getVisionServiceUrl()), file, type, withPosition);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return "";
        }
    }

    public String drawContour(MultipartFile file, String posterUrl) {
        try {
            return visionHubFeignClient.drawContour(new URI(properties.getVisionServiceUrl()), file, posterUrl);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return "";
        }
    }

    public String checkSimilarity(MultipartFile[] file) {
        try {
            return visionHubFeignClient.checkSimilarity(new URI(properties.getVisionServiceUrl()), file, 1);
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return "";
        }
    }

    public R checkSimilarityResult(MultipartFile[] file) {
        String result = checkSimilarity(file);
        // 通过base64来转化图片
        List<PosterSimilarityResultModel> similarPosters = PosterSimilarityHandler.parseResult(result, file);
        return R.ok().data(similarPosters);

    }
}
